package io.github.junxworks.qt.modules.stock.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import io.github.junxworks.qt.modules.stock.dto.TsQueryCondition;
import io.github.junxworks.qt.modules.stock.vo.DStockBlockTradeVo;
import io.github.junxworks.qt.modules.stock.vo.DStockCompanyVo;
import io.github.junxworks.qt.modules.stock.vo.DStockFinaMainbzVo;
import io.github.junxworks.qt.modules.stock.vo.DStockForecastVo;
import io.github.junxworks.qt.modules.stock.vo.DStockHoldernumberVo;
import io.github.junxworks.qt.modules.stock.vo.DStockHoldertradeVo;
import io.github.junxworks.qt.modules.stock.vo.DStockRepurchaseVo;
import io.github.junxworks.qt.modules.stock.vo.DStockToptenFloatholdersVo;
import io.github.junxworks.qt.modules.stock.vo.DStockToptenHoldersVo;

@Mapper
public interface StockMapper {

	@Select("""
			select * from d_stock_forecast
			where ts_code = #{cond.tsCode}
			and end_date >= #{cond.startDate}
			and end_date <= #{cond.endDate}
			order by end_date desc,id asc
			""")
	List<DStockForecastVo> queryStockForecast(@Param("cond") TsQueryCondition condition);

	@Select("""
			select * from d_stock_fina_mainbz
			where ts_code = #{cond.tsCode}
			and end_date >= #{cond.startDate}
			and end_date <= #{cond.endDate}
			order by end_date desc,id asc
			""")
	List<DStockFinaMainbzVo> queryMainBz(@Param("cond") TsQueryCondition condition);

	@Select("select * from d_stock_company where ts_code=#{tsCode}")
	DStockCompanyVo queryStockCompany(String tsCode);

	@Select("""
			select * from d_stock_block_trade
			where ts_code = #{cond.tsCode}
			and trade_date >= #{cond.startDate}
			and trade_date <= #{cond.endDate}
			order by trade_date desc,id asc
			""")
	List<DStockBlockTradeVo> queryBlockTrade(@Param("cond") TsQueryCondition condition);

	@Select("""
			select * from d_stock_topten_floatholders
			where ts_code = #{cond.tsCode}
			and end_date >= #{cond.startDate}
			and end_date <= #{cond.endDate}
			order by end_date desc,hold_float_ratio desc,hold_ratio desc
			""")
	List<DStockToptenFloatholdersVo> queryFloatHolders(@Param("cond") TsQueryCondition condition);

	@Select("""
			select * from d_stock_repurchase
			where ts_code = #{cond.tsCode}
			and ann_date >= #{cond.startDate}
			and ann_date <= #{cond.endDate}
			order by ann_date desc,id desc
			""")
	List<DStockRepurchaseVo> queryRepurchase(@Param("cond") TsQueryCondition condition);

	@Select("""
			select * from d_stock_holdertrade
			where ts_code = #{cond.tsCode}
			and ann_date >= #{cond.startDate}
			and ann_date <= #{cond.endDate}
			order by ann_date desc,id desc
			""")
	List<DStockHoldertradeVo> queryHoldertrade(@Param("cond")TsQueryCondition condition);

	@Select("""
			select
				distinct ts_code,
				end_date ,
				holder_num
			 from d_stock_holdernumber
			where ts_code = #{cond.tsCode}
			and ann_date >= #{cond.startDate}
			and ann_date <= #{cond.endDate}
			order by end_date asc
			""")
	List<DStockHoldernumberVo> queryHoldernumber(@Param("cond")TsQueryCondition condition);
	
	@Select("""
			select
				distinct
				end_date ,
				holder_name ,
				round(hold_ratio,2) hold_ratio
			 from d_stock_topten_holders
			where ts_code = #{cond.tsCode}
			and end_date >= #{cond.startDate}
			and end_date <= #{cond.endDate}
			and hold_ratio != -2147483648
			order by end_date asc
			""")
	List<DStockToptenHoldersVo> queryHolders(@Param("cond")TsQueryCondition condition);
}
